<?php
class ControllerModuleNews extends Controller
{
	public function index()
	{
		$mediaid = $this->request->get['mediaid'];
		
		if($mediaid == "")
		{
			$this->getList();
		}
		else
		{
			$this->getForm();
		}
	}
	
	private function getList()
	{
		$this->load->model("core/media");
		$this->load->helper('image');
		
		$sitemapid = $this->document->sitemapid;
		$mediaid = $this->request->get['mediaid'];
		$siteid = $this->user->getSiteId();
		$step = (int)$this->request->get['step'];
		$to = 5;
		
		//Get list
		$queryoptions = array();
		$queryoptions['mediaparent'] = '%';
		$queryoptions['mediatype'] = '%';
		$queryoptions['refersitemap'] = $sitemapid;
		
		if($mediaid == "")
		{
			$medias = $this->model_core_media->getPaginationList($queryoptions, $step, $to);
			
			if(count($medias) == 1)
			{
				
			}
			
			$this->data = $this->model_core_media->viewNews($medias,180,180);
		}
		$querystring = "index.php?route=page/detail&sitemapid=".$sitemapid;
		
		$pagelinks = $this->model_core_media->getPaginationLinks($index, $queryoptions, $querystring, $step, $to);
		
		$this->data['nextlink'] = $pagelinks['nextlink'];
		$this->data['prevlink'] = $pagelinks['prevlink'];
		
		//Other news
		$this->data['othernews'] = $this->model_core_media->getPaginationList($queryoptions, $step+1, $to);
		for($i=0;$i<count($obj['othernews']);$i++)
		{
			$this->data['othernews'][$i]['statusdate'] = $this->date->formatMySQLDate($obj['othernews'][$i]['statusdate'], 'longdate', "/");
			$this->data['othernews'][$i]['link'] = HTTP_SERVER."index.php?route=page/detail&sitemapid=".$sitemapid."&mediaid=".$obj['othernews'][$i]['mediaid'];
		}
		
		
		$this->id="news";
		$this->template="module/news.tpl";
		$this->render();
	
	}
	
	private function getForm()
	{
		$this->load->model("core/media");
		$this->load->helper('image');
		
		$sitemapid = $this->document->sitemapid;
		$mediaid = $this->request->get['mediaid'];
		$siteid = $this->user->getSiteId();
		
		$this->data['post'] = $this->model_core_media->getItem($mediaid);
		
		if(count($this->data['post']) == 0)
		{
			$this->data['post']['description'] = "Updating...";
		}
		
		$this->data['post']['description'] = html_entity_decode($this->data['post']['description']);
		
		if($this->data['post']['imagepath'] != "")
		{
			$this->data['post']['imagethumbnail'] = "<img src='".HelperImage::resizePNG($this->data['post']['imagepath'], 180, 180)."' class='ben-alignleft' />";
		}
		
		//Get list
		$queryoptions = array();
		$queryoptions['mediaparent'] = '%';
		$queryoptions['mediatype'] = '%';
		$queryoptions['refersitemap'] = $sitemapid;
		$queryoptions['date'] = $this->data['post']['statusdate'];
		$this->data['othernews'] = $this->model_core_media->getOthersMedia($mediaid, $queryoptions, 8);
		for($i=0;$i<count($this->data['othernews']);$i++)
		{
			$this->data['othernews'][$i]['statusdate'] = $this->date->formatMySQLDate($this->data['othernews'][$i]['statusdate'], 'longdate', "/");
			$this->data['othernews'][$i]['link'] = HTTP_SERVER."index.php?route=page/detail&sitemapid=".$sitemapid."&mediaid=".$this->data['othernews'][$i]['mediaid'];
		}
		
		
		$this->id="news";
		$this->template="module/newsdetail.tpl";
		$this->render();
	}
}
?>